package com.bike.controller.api;

import java.math.BigDecimal;
import java.math.RoundingMode;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bike.cache.UserCache;
import com.bike.controller.BaseController;
import com.bike.entity.HhAdvice;
import com.bike.entity.HhBusiness;
import com.bike.entity.HhBusinessUser;
import com.bike.entity.HhFood;
import com.bike.entity.HhWithdraw;
import com.bike.service.impl.HhAdviceServiceImpl;
import com.bike.service.impl.HhAgentServiceImpl;
import com.bike.service.impl.HhBusinessServiceImpl;
import com.bike.service.impl.HhBusinessUserServiceImpl;
import com.bike.service.impl.HhWithdrawServiceImpl;
import com.bike.util.Constant;
import com.bike.util.PageData;
import com.bike.util.TimeManager;

 
@Controller
@RequestMapping(value="/apiPerson")
public class HhApiPersonController extends BaseController {
	
		@Autowired
		private HhAgentServiceImpl  hhAgentService;
		@Autowired
		private HhWithdrawServiceImpl  hhWithdrawServiceImpl;
		@Autowired
		private HhBusinessServiceImpl  hhBusinessServiceImpl;
		@Autowired
		private HhAdviceServiceImpl  hhAdviceServiceImpl;
		@Autowired
		private HhBusinessUserServiceImpl  hhBusinessUserServiceImpl;
		@Autowired
		private UserCache  usercache;
		/**
		 * 跳转到列表页面
		 */
		@RequestMapping("toList")
		public String toList(){
			return "/information/agent/hhAgent_list";
		}
		/**
		 * 提交建议
		 */
		@RequestMapping("advice")
		public String advice(HttpServletRequest request,HhAdvice advice,Model model){
			PageData pd=PageData.getPageDataByPage(request);
			
			hhAdviceServiceImpl.saveOrUpdate(pd, advice);
			
			return "/h5/personIdea";
		}
		/**
		 * 提现
		 */
		@RequestMapping("withdraw")
		@ResponseBody
		public PageData withdraw(HttpServletRequest request,Model model){
			PageData pd=PageData.getPageDataByPage(request);
			HhBusinessUser businessUser=(HhBusinessUser) request.getAttribute("businessUser");
			String code=pd.getString("code");
			
			//验证验证码
			String codeR=usercache.getLoginCodeCacheValue((businessUser.getPhone())).toString();
			if(!StringUtils.isEmpty(codeR)&&code.equals(codeR)){
				usercache.putLoginCodeCacheValue(businessUser.getPhone(), "");
			}else{
				return getErrorPd("验证码失效,请重新获取");
			}
			//提现记录
			int result=hhWithdrawServiceImpl.insertBusinessWithdraw(businessUser);
			if(result>0){
				return  getSuccessPd();
			}
			return getErrorPd("提现失败");
		}
		/**
		 * 去提现
		 */
		@RequestMapping("toWithdraw")
		@ResponseBody
		public PageData toWithdraw(HttpServletRequest request,Model model){
			PageData pd=PageData.getPageDataByPage(request);
			PageData result=getSuccessPd();
			 
			HhBusinessUser businessUser=(HhBusinessUser) request.getAttribute("businessUser");
			HhBusiness business=hhBusinessServiceImpl.selectByHhBusinessId(businessUser.getBusinessId());
			
			BigDecimal serviceFee=null;
			BigDecimal trueAccount=business.getBalance();
			
			BigDecimal waitWithdrawMoney=business.getBalance();
			String bank=business.getBank();
			String bankCode=business.getBankCode();
			String bankName=business.getBankName();
			String bankBranch=business.getBankBranch();
			
			if(businessUser.getRole().equals(Constant.employ)){
				waitWithdrawMoney=businessUser.getBalance();
				bank=businessUser.getBank();
				bankCode=businessUser.getBankCode();
				bankName=businessUser.getBankName();
				bankBranch=businessUser.getBankBranch();
			}
			
			if(business.getPayWithdraw()==0){
				serviceFee=waitWithdrawMoney.multiply(new BigDecimal(Constant.WXwithdrawFee)).setScale(2, RoundingMode.HALF_UP);
				if(serviceFee.doubleValue()==0){
					serviceFee=new BigDecimal("0.01");
				}
				trueAccount=waitWithdrawMoney.subtract(serviceFee);
				model.addAttribute("serviceFee",serviceFee.doubleValue());
			}
			trueAccount=trueAccount.subtract(new BigDecimal("2"));//银联转账手续费
			businessUser=hhBusinessUserServiceImpl.selectByHhBusinessUserId(businessUser.getBusinessUserId());
			
			PageData param=new PageData();
			param.put("trueAccount",trueAccount.doubleValue());
			param.put("business",business);
			param.put("bank",bank);
			param.put("waitWithdrawMoney",waitWithdrawMoney);
			param.put("bankCode",bankCode);
			param.put("bankName",bankName);
			param.put("bankBranch",bankBranch);
			param.put("mobile",businessUser.getPhone());
			result.put("data", param);
			return result;
		}
		/**
		 *  添加银行卡
		 * param   bankName 持卡人姓名
		 * param   bankCard  卡号
		 * param   bank  开户行
		 * param   bankBranch  开户分行
		 */
		@RequestMapping("addBank")
		@ResponseBody
		public PageData addBank(HttpServletRequest request,HhFood food,Model model){
			PageData pd=PageData.getToPageData(request);
			HhBusinessUser businessUser=(HhBusinessUser) request.getAttribute("businessUser");
			
			String code=pd.getString("code");
			
			//验证验证码
			String codeR=usercache.getLoginCodeCacheValue((businessUser.getPhone())).toString();
			if(!StringUtils.isEmpty(codeR)&&code.equals(codeR)){
				usercache.putLoginCodeCacheValue(businessUser.getPhone(), "");
			}else{
				return getErrorPd("验证码失效,请重新获取");
			}
			
			if(businessUser.getRole().equals(Constant.boss)){
				HhBusiness business=new HhBusiness();
				business.setBusinessId(businessUser.getBusinessId());
				business.setBank(pd.getString("bank"));
				business.setBankBranch(pd.getString("bankBranch"));
				business.setBankCode(pd.getString("bankCard"));
				business.setBankName(pd.getString("bankName"));
				hhBusinessServiceImpl.updateBusiness(pd, business);
			}else{
				HhBusinessUser businessUserRecord=new HhBusinessUser();
				businessUserRecord.setBusinessUserId(businessUser.getBusinessUserId());
				businessUserRecord.setBank(pd.getString("bank"));
				businessUserRecord.setBankBranch(pd.getString("bankBranch"));
				businessUserRecord.setBankCode(pd.getString("bankCard"));
				businessUserRecord.setBankName(pd.getString("bankName"));
				hhBusinessUserServiceImpl.updateUser(pd, businessUserRecord);
			}
			model.addAttribute("pd", pd);
			return getSuccessPd();
		}
}
